<?php

namespace App\Lib\Redis;

use EasySwoole\Component\Singleton;

class Redis
{
    
    use Singleton;

    public $redis = '';

    public function __construct()
    {
        $instance = \EasySwoole\EasySwoole\Config::getInstance();
        $redisConf = $instance->getConf('REDIS');
        $this->redis = new \Redis();
        $result = $this->redis->connect($redisConf['host'],$redisConf['port'],$redisConf['POOL_MIN_NUM']);
    }
    

    public function __call($name,$arguments)
    {
        return $this->redis->$name(...$arguments);
    }
    
    public function get($name)
    {
        if(empty($name)){
            return '';
        }
        return $this->redis->get($name);
    }

    public function set($key, $value, $time = 0)
    {
        if(empty($key)){
            return '';
        }
        if(is_array($value)){
            $value = json_encode($value);
        }
        if(!$time){
            return $this->redis->set($key, $value);
        }
        return $this->redis->setex($key, $time, $value);
    }

    public function zincrby($key,$number,$member)
    {
        if(empty($key) || empty($member)){
            return false;
        }
        return $this->redis->zincrby($key,$number,$member);
    }

    public function zrevrange($key, $start, $top, $type)
    {
        if(empty($key)){
            return false;
        }
        return $this->redis->zrevrange($key, $start, $top, $type);
    }

}